//
//  GGMainVC.swift
//  DongBaoDemo
//
//  Created by 陈志勇 on 2018/8/4.
//  Copyright © 2018年 陈志勇. All rights reserved.
//

import UIKit

class GGMainVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpComposeBtn()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setUpTabBarItems()
        setUpComposeBtn()
    }


}

extension GGMainVC {
    private func setUpTabBarItems() {
        for i in 0..<tabBar.items!.count {
            let item = tabBar.items![i]
            
            if i == 2 {
                item.isEnabled = false
                continue
            }
            
            item.selectedImage = UIImage(named: "tab" + "\(i)" + "_p")
            
        }
    }
    
    private func setUpComposeBtn() {
        // 1. 添加到tabbar中去
        let button = UIButton(type: .custom)
        guard let normalImage = UIImage(named: "tabbar_add") else {
            return
        }
        button.frame = CGRect(x: 0, y: 0, width: normalImage.size.width, height: normalImage.size.height)
        button.setImage(normalImage, for: .normal)
        tabBar.addSubview(button)
        
        // 2. 设置位置
        button.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.height * 0.3)
        
        // 3. 监听按钮的点击事件
        button.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
    }
    
    @objc private func composeBtnClick() {
        let composeVc = GGComposeVC()
        composeVc.view.backgroundColor = UIColor.lightGray
//        let composeNav = UINavigationController(rootViewController: composeVc)
        
        present(composeVc, animated: true, completion: nil)
        
    }
}
